package com.lgqz.himalayafm.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lgqz.himalayafm.R;
import com.lgqz.himalayafm.adapter.ProgramAdapter;
import com.lgqz.himalayafm.bean.AlbumDetails;
import com.lgqz.himalayafm.bean.TrackListItem;
import com.lgqz.himalayafm.http.HttpService;
import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class AlbumProgramFragment extends Fragment implements Callback<AlbumDetails>, PullLoadMoreRecyclerView
        .PullLoadMoreListener {


    private TextView mTotal;
    private TextView mSort;
    private TextView mSelect;
    private PullLoadMoreRecyclerView mList;
    private List<TrackListItem> list = new ArrayList<>();
    private ProgramAdapter programAdapter;

    public AlbumProgramFragment() {
        // Required empty public constructor
    }

    public static AlbumProgramFragment newInstance(long albumId) {
        Bundle args = new Bundle();
        args.putLong("albumId", albumId);
        AlbumProgramFragment fragment = new AlbumProgramFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        long albumId = getArguments().getLong("albumId");
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HttpService service = retrofit.create(HttpService.class);
        service.getAlbumProgram(albumId, "android", null, "20").enqueue(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_album_program, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTotal = ((TextView) view.findViewById(R.id.tv_total_album_program));
        mSort = ((TextView) view.findViewById(R.id.tv_sort_album_program));
        mSelect = ((TextView) view.findViewById(R.id.tv_select_album_program));
        mList = ((PullLoadMoreRecyclerView) view.findViewById(R.id.rv_list_album_program));
        mList.setLinearLayout();
        mList.setPullRefreshEnable(false);
        mList.setOnPullLoadMoreListener(this);
        programAdapter = new ProgramAdapter(getContext(), list);
        mList.setAdapter(programAdapter);
    }

    @Override
    public void onResponse(Call<AlbumDetails> call, Response<AlbumDetails> response) {
        List<TrackListItem> itemList = response.body().getData().getTracks().getList();
        programAdapter.addAll(itemList);
        mTotal.setText(response.body().getData().getTracks().getTotalCount() + "");
    }

    @Override
    public void onFailure(Call<AlbumDetails> call, Throwable t) {

    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMore() {

    }
}
